/**
 * @desc index
 * @author DragonTeam <https://www.bpmport.com>
 * @since 2024/11/4 15:26
 */
import request from '@/utils/request'
import { treeDataTranslate } from '@/utils/tools'

const bpmnUrl = '/flow/bpmnDesigner/prod/api'

const API = {
  GetOrgTree: `${bpmnUrl}/getOrgTree`,
  GetPersonalPagerModel: `${bpmnUrl}/getPersonalPagerModel`,
  GetMatrixRoles: `${bpmnUrl}/getMatrixRoles`,
  GetRolePagerModel: `${bpmnUrl}/getRolePagerModel`,
  GetAuthPointVos: `${bpmnUrl}/getAuthPointVos`,
} as const

/**
 * 获取组织（公司+部门）数据 - 树形数据
 */
export async function getOrgTree(): Promise<any[]> {
  try {
    const res = await request({
      method: 'GET',
      url: API.GetOrgTree,
    })
    const treeData = treeDataTranslate(res.data, 'id', 'pid')
    return Promise.resolve(treeData)
  }
  catch (e) {
    console.error(e)
    return Promise.reject(e)
  }
}

/**
 * 获取人员列表
 */
export async function getPersonalPagerModel(params) {
  return request({
    method: 'POST',
    url: API.GetPersonalPagerModel,
    data: { query: params.pager, entity: params.entity },
  })
    .then((res) => {
      const { rows = [] } = res.data
      if (rows) {
        res.data.rows = rows.map((item) => {
          return {
            id: item.id,
            name: item.name,
            code: item.code,
            mobile: item.mobile,
            email: item.email,
            companyName: item.companyName,
            deptName: item.deptName,
          }
        })
      }
      return Promise.resolve(res)
    })
    .catch((e) => {
      return Promise.reject(e)
    })
}

/**
 * 获取矩阵角色列表
 */
export function getMatrixRoles(params) {
  return request({
    method: 'POST',
    url: `${API.GetMatrixRoles}/${params.roleType}`,
  })
}

/**
 * 获取角色分页数据
 */
export async function getRolePagerModel(params) {
  return request({
    method: 'POST',
    url: API.GetRolePagerModel,
    data: { query: params.pager, entity: params.entity },
  })
    .then((res) => {
      const { rows } = res.data
      if (rows) {
        res.data.rows = rows.map((item) => {
          return {
            id: item.id,
            name: item.name,
            sn: item.sn,
            companyId: item.companyId,
            companyName: item.companyName,
          }
        })
      }
      return Promise.resolve(res)
    })
    .catch((e) => {
      return Promise.reject(e)
    })
}

/**
 * 获取用户任务操作项
 */
export function getAuthPointVos() {
  return request({
    method: 'POST',
    url: API.GetAuthPointVos,
  })
}
